﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Pasteles
{
    public partial class SettingsForm : Form
    {
        string sConfig_hostname;
        string sConfig_dbname;
        string sConfig_dbusername;
        string sConfig_dbpassword;
        string sConfig_SerialPort;
        string sConfig_Baudrate;
        string sConfig_Parity;
        string sConfig_Datalength;
        string sConfig_Stopbits;
        public static string sConfig_SafePrinting;
        string sConfig_line1 = "";
        string sConfig_line2 = "";
        string sConfig_line3 = "";
        string sConfig_line4 = "";
        string sConfig_line5 = "";

        public SettingsForm()
        {
            InitializeComponent();
            loadingParameters();
        }


        

        private void LoadConfigBtn_Click(object sender, EventArgs e)
        {
            loadingParameters();
            
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void SaveConfigBtn_Click(object sender, EventArgs e)
        {
            string newValue = string.Empty;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("config.xml");
                XmlNode node = xmlDoc.SelectSingleNode("Pasteles");

                node.Attributes[0].Value = DBServerHostnameTxt.Text;
                node.Attributes[1].Value = DBNameTB.Text;
                node.Attributes[2].Value = DBUsername.Text;
                node.Attributes[3].Value = DBPassword.Text;
                XmlNode node22 = xmlDoc.SelectSingleNode("Pasteles/Printing");
                node22.Attributes[0].Value = ComPorts.Text;
                node22.Attributes[1].Value = baudrate.Text;
                node22.Attributes[2].Value = parity.Text;
                node22.Attributes[3].Value = datalength.Text;
                node22.Attributes[4].Value = stopbits.Text;

                XmlNode node1 = xmlDoc.SelectSingleNode("Pasteles/Printing/line1");
                node1.InnerText = BillHeaderLine1TB.Text;
                XmlNode node2 = xmlDoc.SelectSingleNode("Pasteles/Printing/line2");
                node2.InnerText = BillHeaderLine2TB.Text;
                XmlNode node3 = xmlDoc.SelectSingleNode("Pasteles/Printing/line3");
                node3.InnerText = BillHeaderLine3TB.Text;
                XmlNode node4 = xmlDoc.SelectSingleNode("Pasteles/Printing/line4");
                node4.InnerText = BillHeaderLine4TB.Text;
                XmlNode node5 = xmlDoc.SelectSingleNode("Pasteles/Printing/line5");
                node5.InnerText = BillHeaderLine5TB.Text;


                xmlDoc.Save("config.xml");
                
            }
            catch (Exception error)
            {
                MessageBox.Show("Saving XML config file error: " + error.ToString());
            }


        }

        private void TestConnectionBtn_Click(object sender, EventArgs e)
        {
            string MyConString = "SERVER=" + DBServerHostnameTxt.Text + ";" +
                "DATABASE=" + DBNameTB.Text + ";" +
                "UID=" + DBUsername.Text + ";" +
                "PASSWORD=" + DBPassword.Text + ";";
            MySqlConnection connection = new MySqlConnection(MyConString);
            //MySqlCommand command = connection.CreateCommand();
            //MySqlDataReader Reader;

            try
            {
                connection.Open();
                ConnectionStatusLabel.Text = "Connection is OK!";
            }
            catch
            {
                ConnectionStatusLabel.Text = "Connection Errors!";
                Console.WriteLine("Connection Error");
            }

            connection.Close();
        }

        /// <summary>
        /// /////////////////////////////////////////////////////////////////////
        /// </summary>
        private void loadingParameters()
        {
            XmlReader reader = XmlReader.Create("config.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                    && reader.Name == "Pasteles")
                {
                    /*
                     * MessageBox.Show("hostname= " + reader.GetAttribute(0));
                    MessageBox.Show("dbname= " + reader.GetAttribute(1));
                    MessageBox.Show("dbusername= " + reader.GetAttribute(2));
                    MessageBox.Show("dbpassword= " + reader.GetAttribute(3));
                    */
                    sConfig_hostname = reader.GetAttribute(0);
                    sConfig_dbname = reader.GetAttribute(1);
                    sConfig_dbusername = reader.GetAttribute(2);
                    sConfig_dbpassword = reader.GetAttribute(3);


                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "Printing")
                        {
                             
                            sConfig_SerialPort = reader.GetAttribute(0);
                            sConfig_Baudrate = reader.GetAttribute(1);
                            sConfig_Parity = reader.GetAttribute(2);
                            sConfig_Datalength = reader.GetAttribute(3);
                            sConfig_Stopbits = "One";
                            sConfig_SafePrinting = reader.GetAttribute(4);

                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.Name == "line1")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            // MessageBox.Show("line1 = " + reader.Value);
                                            sConfig_line1 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if

                                if (reader.Name == "line2")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line2 = " + reader.Value);
                                            sConfig_line2 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line3")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line3 = " + reader.Value);
                                            sConfig_line3 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line4")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line4 = " + reader.Value);
                                            sConfig_line4 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line5")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line5 = " + reader.Value);
                                            sConfig_line5 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                            }
                        } //end if
                    } //end while
                } //end if
            } //end while

            DBServerHostnameTxt.Text = sConfig_hostname;
            DBNameTB.Text = sConfig_dbname;
            DBUsername.Text = sConfig_dbusername;
            DBPassword.Text = sConfig_dbpassword;
            ComPorts.Text = sConfig_SerialPort;
            baudrate.Text = sConfig_Baudrate;
            parity.Text = sConfig_Parity;
            datalength.Text = sConfig_Datalength;
            stopbits.Text = sConfig_Stopbits;
            BillHeaderLine1TB.Text = sConfig_line1;
            BillHeaderLine2TB.Text = sConfig_line2;
            BillHeaderLine3TB.Text = sConfig_line3;
            BillHeaderLine4TB.Text = sConfig_line4;
            BillHeaderLine5TB.Text = sConfig_line5;
            reader.Close();

        }// End loadingParameter()
    }
}
